#include "FeedFrameHandler.h"

enum FeedIds
{
	// id for sockets
	FEED_TIMER_ID = 2001
};

BEGIN_EVENT_TABLE( FeedFrameHandler, wxFrame )
EVT_TIMER(FEED_TIMER_ID,FeedFrameHandler::RefreshUI)
EVT_SIZE(FeedFrameHandler::OnResize)
/*EVT_BUTTON(wxID_FREEZE,PlotFrameHandler::OnFreeze)
EVT_BUTTON(wxID_PLAY,PlotFrameHandler::OnPlay)*/
END_EVENT_TABLE()
FeedFrameHandler::FeedFrameHandler(wxWindow *parent, int id,int Feedbacks,int SampleFreq, int PlotSeconds) : FeedFrame(parent,id)
{
	MySampleFreq	=	SampleFreq;
	m_SigPlot = NULL;
	//pDataSplit = pDataMan;
	OldTrigger = 0;
	
	m_StatsGrid->SetColLabelValue(0,_T("Calls"));
	m_StatsGrid->SetColLabelValue(1,_T("Execution Time (ms)"));
	m_StatsGrid->SetColLabelValue(2,_T("Frequency (Hz)"));
	m_StatsGrid->SetRowLabelValue(0,_T("Main window"));
	m_StatsGrid->SetRowLabelValue(1,_T("Short window"));

	m_StatsGrid->SetCellValue(0,0,wxString::Format(_("0")));
	m_StatsGrid->SetCellValue(0,1,wxString::Format(_("0")));
	m_StatsGrid->SetCellValue(0,2,wxString::Format(_("0")));
	m_StatsGrid->SetCellValue(1,0,wxString::Format(_("0")));
	m_StatsGrid->SetCellValue(1,1,wxString::Format(_("0")));
	m_StatsGrid->SetCellValue(1,2,wxString::Format(_("0")));
	m_StatsGrid->DisableCellEditControl();
	m_StatsGrid->AutoSize();
	Layout();

	memset(&Stats,0,sizeof(Stats));

	m_HimTrigger->SetLabel	(_("HIM trigger:     0"));
	m_AEnimaTrigger->SetLabel(_("AEnima trigger:  0"));
	m_HWTrigger->SetLabel	(_("HW trigger:      0"));
	m_LastClass->SetLabel(wxString::Format(_("Last classification: 0")));
	

	PlotLength = SampleFreq*PlotSeconds;

	//pFeedbackCurve[0] = new GangiaPlotCurve(0,0,100,FEED_X_MIN,FEED_X_LENGTH,5,wxColour(0,0,0));
	FeedNumber = Feedbacks;
	
	for(int i=0;i<=Feedbacks;i++)
	{
		pFeedbackCurve[i] = new SignalClass();	
		pFeedbackCurve[i]->SetSignal(0,100,1,BLUE,wxString::Format(_("%d"),i),_T("perc %"),500,PlotSeconds,SampleFreq);
	}
	
	StartDrawing();
}


void FeedFrameHandler::SetFeedback(float Feed[])
{
	for(int i=0;i<FeedNumber;i++) pFeedbackCurve[i]->AddData(Feed[i]);
}
void FeedFrameHandler::SetClassification(int Class)
{
	if(Class != 0) 
	{
		m_LastClass->SetLabel(wxString::Format(_("Last classification: %d"),Class));
	}
	if (this->IsShown())
	{
		if (Class != 4) //Non disegna il centro
			m_SigPlot->SetTrigger(1,RED,wxString::Format(_("%d"),Class));
	}
}
void FeedFrameHandler::SetTrigger(int Man,int AEnima, int HW)
{
	if(Man!=0)m_HimTrigger->SetLabel(wxString::Format(_("HIM trigger:     %d"),Man));
	if(AEnima!=0)m_HWTrigger->SetLabel(wxString::Format(_("AEnima trigger:  %d"),AEnima));
	if(HW!=0)m_AEnimaTrigger->SetLabel(wxString::Format(_("HW trigger:      %d"),HW));
}
FeedFrameHandler::~FeedFrameHandler(void)
{
/*
	for(int i=0;i<=FeedNumber;i++)
	{
		delete pFeedbackCurve[i];
	}*/
	Timer.Stop();
}

void FeedFrameHandler::StartDrawing(void)
{
	//if(pDataSplit == NULL) return;
	//m_FeedPlot->AddCurve(pDataSplit->pTheCursor,true,true);
	
	Timer.SetOwner( this,FEED_TIMER_ID);
	Timer.Start(200);
/*	m_PlayButton->Disable();
	m_FreezeButton->Enable();
	m_ReshapeButton->Enable();*/
}
void FeedFrameHandler::RefreshUI(wxTimerEvent& Event)
{
	Event.Skip();
	if(m_SigPlot != NULL)	
	{
		m_SigPlot->DrawAll();
	}

	m_StatsGrid->SetCellValue(0,0,wxString::Format(_("%d"),Stats.CountMain));
	m_StatsGrid->SetCellValue(0,1,wxString::Format(_("%f"),Stats.TimeMain));
	m_StatsGrid->SetCellValue(0,2,wxString::Format(_("%f"),Stats.FreqMain));

	m_StatsGrid->SetCellValue(1,0,wxString::Format(_("%d"),Stats.CountSec));
	m_StatsGrid->SetCellValue(1,1,wxString::Format(_("%f"),Stats.TimeSec));
	m_StatsGrid->SetCellValue(1,2,wxString::Format(_("%f"),Stats.FreqSec));
}

void FeedFrameHandler::OnResize(wxSizeEvent& event)
{
	//Timer.Stop();
	if(IsShown())
	{
		Layout();
		wxSize newSize = m_HelpPanel->GetSize();
		if(m_SigPlot != NULL)
		{
			PlotSizer->Detach(m_SigPlot);
			delete m_SigPlot;
		}	
		m_SigPlot = new Plot(m_HelpPanel,newSize.x,newSize.y,4,PlotLength,L"sec",L"perc %",5,true,true/*,CurveCount*/);
		for(int i = 0;i<FeedNumber;i++)
		{
			m_SigPlot->AddCurve(pFeedbackCurve[i]);
		}
		PlotSizer->Add(m_SigPlot,1,wxEXPAND);
		PlotSizer->Layout();
		bSizer5->Layout();
		Layout();
	}
}
